//公告路由
const express = require("express");
const router = express();
const Notice = require("../model/Notice")

//添加公告
router.post("/addNotice", async(req, res, next) => {
  const data = await Notice.create(req.body);
  res.send({
      status: "ok",
      data: data,
  })
});

//删除公告
router.delete('/delNotice/:id', async(req, res) => {
  await Notice.findByIdAndDelete(req.params.id)
  res.send({
      status: "ok"
  })
})

//分页查询列表+姓名+学号模糊查询:
router.get('/listNotice', async(req, res) => {
  console.log(req.query)
  page = parseInt(req.query.page);
  limit = parseInt(req.query.limit);  
  const total = await Notice.countDocuments({nickname:{$regex: req.query.nickname,$options: 'i'},username:{$regex: req.query.username}})
  const data = await Notice.find({nickname:{$regex: req.query.nickname,$options: 'i'},username:{$regex: req.query.username}}).sort({_id: -1}).skip((page - 1) * limit).limit(limit) 
  res.send({
      status: "ok",
      data: data,total
  })
})

//以下为测试接口

//全部列表信息
router.get('/list', async(req, res) => {
  const data = await Notice.find()
  res.send({
      status: "ok",
      data: data,
  })
})

//删库
router.post('/test', async(req, res) => {
  console.log(req.body)
  await Notice.deleteMany();
  res.send({
      status: "ok",
  })
})

router.get("/", (req, res) => {
  res.send("hello notice...");
});

module.exports = router;